package org.liuqm.example.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

public class MybatisUtils {

    static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "mybatis-config.xml";
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    public static SqlSession openSession(boolean autoSubmit) {
        return sqlSessionFactory.openSession(autoSubmit);
    }


    public static void closeSession(SqlSession session) {
        if (session != null) {
            session.close();
        }
    }


}
